﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Scheduling.Models.Interfaces;

namespace Scheduling.Models.Repos.InMemory
{
    public class InMemoryBookingLoader : IBookingLoader
    {


        public static List<Booking> BookingList;

        public InMemoryBookingLoader()
        {
            if (BookingList == null)
            {
                BookingList = new List<Booking>();
                Booking b1 = new Booking() { ID = 1, EndDate = new DateTime(2000, 1, 1), StartDate = new DateTime(2001, 1, 1) };
                Booking b2 = new Booking() { ID = 2, EndDate = new DateTime(2002, 1, 1), StartDate = new DateTime(2003, 1, 1) };

                b1.Room = b2.Room = new Room() { ID = "21E404", Name = null, Description = null, Capacity = 32 };
                b1.User = b2.User = new User() { ID = "MBl", FirstName = "Martin", LastName = "Blom", Email = "martin.blom@example.com", Password = "", Phone = "070-0707070" };
                BookingList.Add(b1);
                BookingList.Add(b2);
            }
        }

        public void Clear()
        {
            BookingList = null;
        }

        public List<Booking> GetBookings()
        {

            return BookingList;
        }

        public Booking GetBooking(int BookingID)
        {
            foreach (Booking booking in BookingList)
            {
                if (booking.ID == BookingID)
                    return booking;
            }
            return null;
        }

        public List<Booking> GetBookingsByUserID(string UserID)
        {
            List<Booking> result = new List<Booking>();
            foreach (Booking booking in BookingList)
                if (booking.User.ID == UserID)
                    result.Add(booking);
            return result;
        }

        public List<Booking> GetCollisions(Booking Booking)
        {
            List<Booking> result = new List<Booking>();
            foreach (Booking b in BookingList)
                if (!((b.StartDate < Booking.StartDate && b.EndDate < Booking.StartDate) || (b.StartDate > Booking.EndDate && b.EndDate > Booking.StartDate) || b.Room.ID !=Booking.Room.ID))
                    result.Add(b);
            return result;
        }

        public void DeleteBooking(int BookingID)
        {
            for (int i = 0; i < BookingList.Count; i++)
            {
                if (BookingList[i].ID == BookingID)
                    BookingList.RemoveAt(i);
            }
        }

        public void InsertBooking(Booking Booking)
        {
            BookingList.Add(Booking);
        }
        public void EditBooking(Booking booking)
        {
            DeleteBooking(booking.ID);
            InsertBooking(booking);
        }


        public List<Booking> GetBookingByCourseID(string CourseID)
        {
            throw new NotImplementedException();
        }


        public List<Booking> GetBookingsByCourseInstanceID(int CourseInstanceID)
        {
            throw new NotImplementedException();
        }
    }
}